import {LogLanguage} from "../dist/index.js"

// 综合测试用例 - 包含所有功能
const testCases = [
  // 1. 基本日志格式
  "2024-01-15 10:30:45 [INFO] [TID:] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] [service.SBYBaseService:70] Processing request",
  
  // 2. 参数类型
  "2024-01-15 10:30:45 [INFO] Parameters: points_package_id(String) user_id(Long) is_active(Boolean)",
  
  // 3. SQL语句
  "2024-01-15 10:30:45 [INFO] Preparing: UPDATE users SET status = ? WHERE id = ?",
  
  // 4. 混合格式 - 包含所有功能
  "2024-01-15 10:30:45 [INFO] [TID:] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] Parameters: user_id(Integer) name(String) age(Integer) Executing: SELECT * FROM users WHERE status = ? AND age > ?",
  
  // 5. 复杂SQL
  "2024-01-15 10:30:45 [INFO] CREATE TABLE users (id INTEGER, name VARCHAR(255))",
  
  // 6. 字符串和数字
  "2024-01-15 10:30:45 [INFO] [TID:] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] Processing request with id=\"12345\" and count=100",
  
  // 7. 错误日志
  "2024-01-15 10:30:45 [ERROR] [TID:abc123] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] (com.cn.yft.ora.bank.sby.service.SBYBaseService:70) Database connection failed",
  
  // 8. 完整示例
  "2024-01-15 10:30:45 [INFO] [TID:] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] INSERT INTO logs (message_id(String), level(Integer), timestamp(Long)) VALUES (?, ?, ?) with parameters: user_id=1749611201012113"
]

console.log("=== 综合功能测试 ===")

testCases.forEach((testCase, index) => {
  console.log(`\n测试 ${index + 1}:`)
  console.log(`输入: ${testCase}`)
  
  const tree = LogLanguage.parser.parse(testCase)
  let foundNodes = []
  
  tree.iterate({
    enter: (node) => {
      foundNodes.push({
        name: node.type.name,
        from: node.from,
        to: node.to,
        text: testCase.slice(node.from, node.to)
      })
    }
  })
  
  // 检查各种功能
  const dateNodes = foundNodes.filter(node => node.name === 'DATEExpression')
  const logLevelNodes = foundNodes.filter(node => 
    node.name.includes('Expression') && 
    (node.name.includes('FATAL') || node.name.includes('ERROR') || 
     node.name.includes('INFO') || node.name.includes('WARNING') || 
     node.name.includes('Debug'))
  )
  const classPathNodes = foundNodes.filter(node => node.name === 'CLASSPATHExpression')
  const tidNodes = foundNodes.filter(node => 
    node.name.includes('TID') || 
    node.text.includes('fcdfba1a21214a8e97c7d8fd1a8aa287')
  )
  const stringNodes = foundNodes.filter(node => node.name === 'StringLiteral')
  const numberNodes = foundNodes.filter(node => node.name === 'NumberExpression')
  
  console.log("\n语法解析结果:")
  console.log(`  - 日期识别: ${dateNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 日志级别识别: ${logLevelNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 类路径识别: ${classPathNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - TID识别: ${tidNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 字符串识别: ${stringNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 数字识别: ${numberNodes.length > 0 ? '✓' : '✗'}`)
  
  // 检查参数类型
  const paramTypeRegex = /\b([a-zA-Z_]+)\s*\(([A-Z][a-zA-Z]*)\)/g
  let match
  let foundParamTypes = []
  
  while ((match = paramTypeRegex.exec(testCase)) !== null) {
    foundParamTypes.push({
      full: match[0],
      param: match[1],
      type: match[2]
    })
  }
  
  if (foundParamTypes.length > 0) {
    console.log(`  - 参数类型识别: ✓ (${foundParamTypes.length}个)`)
    foundParamTypes.forEach(param => {
      console.log(`    * ${param.param}(${param.type})`)
    })
  } else {
    console.log(`  - 参数类型识别: ✗`)
  }
  
  // 检查SQL语句
  const sqlRegex = /\b(SELECT|UPDATE|INSERT|DELETE|CREATE|DROP|ALTER)\b/gi
  let foundSQLStatements = []
  
  while ((match = sqlRegex.exec(testCase)) !== null) {
    foundSQLStatements.push({
      keyword: match[1],
      index: match.index
    })
  }
  
  if (foundSQLStatements.length > 0) {
    console.log(`  - SQL语句识别: ✓ (${foundSQLStatements.length}个)`)
    foundSQLStatements.forEach(sql => {
      console.log(`    * ${sql.keyword}`)
    })
  } else {
    console.log(`  - SQL语句识别: ✗`)
  }
  
  // 检查无前缀TID
  const tidWithoutPrefixRegex = /\[([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*(\.[0-9]+)*)\]/g
  let foundTIDsWithoutPrefix = []
  
  while ((match = tidWithoutPrefixRegex.exec(testCase)) !== null) {
    const content = match[1]
    if (!content.startsWith('TID:') && !content.startsWith('tid:')) {
      const isLogLevel = /^(FATAL|ERROR|INFO|WARNING|DEBUG|WARN)$/i.test(content)
      if (!isLogLevel) {
        foundTIDsWithoutPrefix.push(match[0])
      }
    }
  }
  
  if (foundTIDsWithoutPrefix.length > 0) {
    console.log(`  - 无前缀TID识别: ✓ (${foundTIDsWithoutPrefix.length}个)`)
    foundTIDsWithoutPrefix.forEach(tid => {
      console.log(`    * ${tid}`)
    })
  } else {
    console.log(`  - 无前缀TID识别: ✗`)
  }
})
